package com.xywy.bloodandroid.act;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.google.gson.reflect.TypeToken;
import com.xywy.bloodandroid.R;
import com.xywy.bloodandroid.adapter.OnLineMessageAdapter;
import com.xywy.bloodandroid.base.BaseActivity;
import com.xywy.bloodandroid.bean.OnlineQuestionList;
import com.xywy.bloodandroid.bean.User;
import com.xywy.bloodandroid.util.JsonUtil;
import com.xywy.bloodandroid.util.MD5;
import com.xywy.bloodandroid.util.NewFileUtil;
import com.xywy.bloodandroid.util.UIUtil;
import com.xywy.bloodandroid.vollery.OnVolleyResponseListener;
import com.xywy.bloodandroid.vollery.PostRequest;

/**
 * 免费问医生
 */
public class FreeAskDoctorActivity extends BaseActivity {

	private ListView message_listview;
	private List<OnlineQuestionList> list = new ArrayList<OnlineQuestionList>();
	private OnLineMessageAdapter adapter;
	private RelativeLayout left_img;
	private TextView middle_title;
	private String Tag = "FreeAskDoctorActivity";

	private final int page_size = 20;
	private final int page_number = 1;

	@Override
	protected void initView(Bundle savedInstanceState) {
		setContentView(R.layout.activity_free_ask);
		left_img = (RelativeLayout) findViewById(R.id.left_img);
		middle_title = (TextView) findViewById(R.id.middle_title);
		middle_title.setText("\"问医生\"消息");
		message_listview = (ListView) findViewById(R.id.message_listview);
	}

	@Override
	protected void initListener() {
		left_img.setOnClickListener(this);
	}

	@Override
	protected void initData() {
		@SuppressWarnings("unchecked")
		List<OnlineQuestionList> oldList = (List<OnlineQuestionList>) NewFileUtil
				.getObject(NewFileUtil.MESSAGE_ONLINE);
		if (null != oldList && oldList.size() > 0)
			list.addAll(oldList);
		getQuestOnline(page_size, page_number);
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.left_img:
			finish();
			break;
		}
	}

	Handler mHandler = new Handler() {
	};

	/**
	 * 获取在线提问列表信息
	 * 
	 * @param psize
	 * @param pagenume
	 */

	/**
	 * 获取在线提问列表信息
	 * 
	 * @param psize
	 * @param pagenume
	 */

	private void getQuestOnline(int psize, int pagenume) {

		String url = "http://api.yun.xywy.com/index.php/glu_app/ad/msg_box/";
		PostRequest<String> postRequest = new PostRequest<String>(url,
				String.class, new OnVolleyResponseListener<String>() {

					@Override
					public void onError(VolleyError volleyError) {
						UIUtil.DissDialog(context, waittingDialog);
						UIUtil.showToast(context,
								getResources().getString(R.string.network_slow));
					}

					@SuppressWarnings("unchecked")
					@Override
					public void onSuccess(String response) {
						UIUtil.DissDialog(context, waittingDialog);
						try {
							JSONObject jsonObject = new JSONObject(response);
							int state = jsonObject.getInt("state");
							if (state == 200) {
								if (jsonObject.has("data")
										&& jsonObject.getString("data")
												.length() > 0) {
									// 将新获取的数据放在前面；
									list.addAll(
											0,
											(List<OnlineQuestionList>) JsonUtil.parseJsonToList(
													jsonObject
															.getString("data"),
													new TypeToken<List<OnlineQuestionList>>() {
													}.getType()));
									adapter = new OnLineMessageAdapter(
											FreeAskDoctorActivity.this, list);
									message_listview.setAdapter(adapter);
									// 清理 将获取的数据保存到本地
									NewFileUtil
											.clearFile(NewFileUtil.MESSAGE_ONLINE);
									NewFileUtil.saveObject(
											NewFileUtil.MESSAGE_ONLINE, list);

								}
							} else {
								// TODO: 此处展示无数据位置
								UIUtil.showToast(context,
										jsonObject.getString("msg"));
							}
						} catch (JSONException e) {
							e.printStackTrace();
						}

					}
				});
		Map<String, String> params = new HashMap<String, String>();
		params.put("xywy_userid", User.getInstance(context).getUserid());
		params.put("datatime", "1414741230");
		params.put("type", "0");
		postRequest.setParams(params);
		postRequest.setIsParseJson(false); // 如果使用String 不会自动解析json
		executeRequest(postRequest);
	}

	static String url = "http://api.yun.xywy.com/index.php/glu_app/ad";
	public static String Sign = "KHy69gsk8%#@kl$";// 数字签名验证
	public static String tag = "TangAndroid";

	/**
	 * 获取请求的url的拼接
	 * 
	 * @return
	 */
	public static String getUrl(String act, String fun) {

		try {
			String sign = MD5.md5s(Sign + tag);
			if (act != null && fun != null) {
				return url + "&tag=" + tag + "&sign=" + sign + "&act=" + act
						+ "&fun=" + fun;
			} else {
				// 上传图片的
				return "http://api.wws.xywy.com/upload.php?" + "&tag=" + tag
						+ "&sign=" + sign;
			}
		} catch (Exception e) {

			e.printStackTrace();
			return "-100";
		}

	}
}
